package gui;

import controllers.GUIController;
import hibernate.User;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class AAddStudent extends JInternalFrame {

    private JPanel ButtonPanel;
    private JButton addstudentButton;
    private JPanel bkgrd;
    private JTextArea browseField;
    private JButton browseButton;
    private JButton cancelButton;
    private JTextField firstNameField;
    private JLabel firstNameLabel;
    private JButton homeButton;
    private JPanel infoPanel;
    private JPanel jPanel2;
    private JTextField lastNameField;
    private JLabel lastNameLabel;
    private JLabel logo;
    private JPanel logoPanel;
    private JButton logoutButton;
    private JButton myaccountButton;
    private javax.swing.JScrollPane jScrollPane1;
    private JButton activitiesButton;
    private JLabel nicknameLabel;
    private JTextField nicknameField;
    private GUIController guiC;
    final AAddStudent obj = this;
    private User currentStudent;
    private List<User> sList;
    private File imageFile;

    public AAddStudent(GUIController m) {
        guiC = m;
        generateLayout();
        addMyAccountActionListener();
        addHomeActionListener();
        addLogoutActionListener();
        addCancelActionListener();
        addAddStudentActionListener();
        addBrowsePicActionListener();
        addActivitiesActionListener();

      }
    

     private void addActivitiesActionListener() {
        activitiesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AAddActivity(guiC));
                obj.dispose();
            }
        });
    }

    // adds actionlistener to addstudent button to add students to the database
    private void addAddStudentActionListener() {
        addstudentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User u = new User();
                if (firstNameField.getText().equals("") || lastNameField.getText().equals("") || browseField.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Student first and last name and image must be filled in.");
                } else {
                    addStudentInfo(u);
                    u = guiC.copyStudentImageFiles(u.getImage(), u);
                    guiC.saveDbData(u);
                    currentStudent = u;
                    updateStudentList();
                    JOptionPane.showMessageDialog(null, "Student " + currentStudent.getFirstName() + " " + currentStudent.getLastName() + " has been added to the class.");
                    clearFields();
                }
            }
        });
    }
    
    public void clearFields() {
        firstNameField.setText("");
        lastNameField.setText("");
        nicknameField.setText("");
        browseField.setText("");
    }
    
    //user is a student. This function sets all variables for the student being added
    public void addStudentInfo(User u) {
        u.setUserAccess(constants.UserTypes.STUDENT.VALUE());
        u.setFirstName(firstNameField.getText());
        u.setCreateDate(new Date());
        u.setChangedDate(new Date());
        if (nicknameField.getText().equals("")) {
            u.setCommonName(firstNameField.getText());
        } else {
            u.setCommonName(nicknameField.getText());
        }
        u.setFirstName(firstNameField.getText());
        u.setLastName(lastNameField.getText());
        if(browseField.getText().equals("")){

        }
        u.setImagePath(browseField.getText());
        u.setImage(imageFile);
        u.setImagePath(browseField.getText());
        u.setTeacherId(guiC.currentUser.getUserId());
    }

    
    private void updateStudentList() {
        List tempList = new ArrayList();
        tempList = guiC.getStudentList(guiC.currentUser);
        sList = tempList;
    }

    // adds actionlistener to the logout button to log the admin out of their
    // account
    private void addLogoutActionListener() {
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // show login page/close application on logout
                guiC.showGUI(new ALogin(guiC));
                // close previous gui(AMyClass)
                obj.dispose();
            }
        });
    }

    // adds actionlistener to myaccount button to go to the page My Class
    private void addMyAccountActionListener() {
        myaccountButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    guiC.showGUI(new AMyAccount(guiC));
                } catch (IOException ex) {
                    Logger.getLogger(AAddStudent.class.getName()).log(Level.SEVERE, null, ex);
                }
                obj.dispose();
            }
        });
    }

    //adds actionlistener to home button to go to the home page
    private void addHomeActionListener() {
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AMyClass(guiC));
                obj.dispose();
            }
        });
    }

    //adds actionlistener to cancel button to go to the home page
    private void addCancelActionListener() {
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AEditUser(guiC));
                obj.dispose();
            }
        });
    }

    public void addBrowsePicActionListener() {
        browseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                int retVal = fc.showOpenDialog(null);
                if (retVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    browseField.append(file.getName());  
                    imageFile = file;
                }
            }
        });
    }    

    public void generateLayout() {
        bkgrd = new javax.swing.JPanel();
        logoPanel = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        ButtonPanel = new javax.swing.JPanel();
        homeButton = new javax.swing.JButton();
        myaccountButton = new javax.swing.JButton();
        logoutButton = new javax.swing.JButton();
        activitiesButton = new javax.swing.JButton();
        infoPanel = new javax.swing.JPanel();
        firstNameLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        browseButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        browseField = new javax.swing.JTextArea();
        addstudentButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        firstNameField = new javax.swing.JTextField();
        lastNameLabel = new javax.swing.JLabel();
        lastNameField = new javax.swing.JTextField();
        nicknameLabel = new javax.swing.JLabel();
        nicknameField = new javax.swing.JTextField();
        
        browseField.setEditable(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OnPoint Scheduler- Main");

       bkgrd.setBackground(new java.awt.Color(0, 102, 255));
        
        bkgrd.setToolTipText("");
        
        logoPanel.setOpaque(false);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/OnPointlogo2-1.png"))); // NOI18N

        javax.swing.GroupLayout logoPanelLayout = new javax.swing.GroupLayout(logoPanel);
        logoPanel.setLayout(logoPanelLayout);
        logoPanelLayout.setHorizontalGroup(
            logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(logoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logo)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        logoPanelLayout.setVerticalGroup(
            logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(logo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        ButtonPanel.setOpaque(false);

        homeButton.setText("Home");
        homeButton.setOpaque(false);
        
        myaccountButton.setText("MyAccount");
        myaccountButton.setOpaque(false);

        logoutButton.setText("Logout");
        logoutButton.setOpaque(false);

        activitiesButton.setText("Activities");
        activitiesButton.setOpaque(false);

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(myaccountButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(homeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(logoutButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(activitiesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(homeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(activitiesButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myaccountButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logoutButton)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        infoPanel.setBackground(new java.awt.Color(51, 153, 255));
        infoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add a Student", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 1, 14))); // NOI18N

        firstNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        firstNameLabel.setText("First Name: ");

        jPanel2.setBackground(new java.awt.Color(51, 153, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add Student Picture", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 1, 14))); // NOI18N

        browseButton.setText("Browse");
        browseButton.setOpaque(false);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        browseField.setColumns(20);
        browseField.setRows(5);
        jScrollPane1.setViewportView(browseField);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(browseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                .addGap(18, 18, 18))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(browseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(107, Short.MAX_VALUE))
        );

        addstudentButton.setText("Add Student");
        addstudentButton.setOpaque(false);

        cancelButton.setText("Cancel");
        cancelButton.setOpaque(false);

        firstNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        lastNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lastNameLabel.setText("Last Name: ");

        lastNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        nicknameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        nicknameLabel.setText("Nickname: ");

        javax.swing.GroupLayout infoPanelLayout = new javax.swing.GroupLayout(infoPanel);
        infoPanel.setLayout(infoPanelLayout);
        infoPanelLayout.setHorizontalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infoPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(addstudentButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addGroup(infoPanelLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 11, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(nicknameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(firstNameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lastNameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(firstNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                    .addComponent(lastNameField)
                    .addComponent(nicknameField))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        infoPanelLayout.setVerticalGroup(
            infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNameLabel)
                    .addComponent(firstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nicknameLabel)
                    .addComponent(nicknameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(infoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(addstudentButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout bkgrdLayout = new javax.swing.GroupLayout(bkgrd);
        bkgrd.setLayout(bkgrdLayout);
        bkgrdLayout.setHorizontalGroup(
            bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(bkgrdLayout.createSequentialGroup()
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(446, Short.MAX_VALUE))
        );
        bkgrdLayout.setVerticalGroup(
            bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(bkgrdLayout.createSequentialGroup()
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(311, 311, 311))
                    .addGroup(bkgrdLayout.createSequentialGroup()
                        .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
      
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bkgrd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bkgrd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            
        );
        

        pack();
    }
}